package ian.android.lovewords.ui;

import ian.android.lovewords.R;
import ian.android.lovewords.words.*;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class BlocksLayout extends ViewGroup {
    private int mColumns = 3;

    private boolean isAdded = false;
    public BlockView blockView;
    private TimeRulerView mRulerView;
    private View mNowView;
    private Context context;
    private WordSchedule schedule;

    public BlocksLayout(Context context) {
        this(context, null);
        this.context = context;
        initSchedule();
    }

    public BlocksLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
        initSchedule();
    }

    public BlocksLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        final TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.BlocksLayout, defStyle, 0);

        mColumns = a.getInt(R.styleable.TimeRulerView_headerWidth, mColumns);
        this.context = context;
        a.recycle();
        initSchedule();
    }

    private void initSchedule()
    {
    	schedule = new WordSchedule();
    }
    
    private void ensureChildren() {
        mRulerView = (TimeRulerView) findViewById(R.id.blocks_ruler);
        if (mRulerView == null) {
            throw new IllegalStateException("Must include a R.id.blocks_ruler view.");
        }
        mRulerView.setDrawingCacheEnabled(true);

        mNowView = findViewById(R.id.blocks_now);
        if (mNowView == null) {
            throw new IllegalStateException("Must include a R.id.blocks_now view.");
        }
        mNowView.setDrawingCacheEnabled(true);
    }

    /**
     * Remove any {@link BlockView} instances, leaving only
     * {@link TimeRulerView} remaining.
     */
    public void removeAllBlocks() {
        ensureChildren();
        removeAllViews();
        addView(mRulerView);
        addView(mNowView);
    }

    public void addBlock(BlockView blockView) {
        blockView.setDrawingCacheEnabled(true);
        addView(blockView, 1);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        ensureChildren();

        mRulerView.measure(widthMeasureSpec, heightMeasureSpec);
        mNowView.measure(widthMeasureSpec, heightMeasureSpec);

        final int width = mRulerView.getMeasuredWidth();
        final int height = mRulerView.getMeasuredHeight();

        setMeasuredDimension(resolveSize(width, widthMeasureSpec),
                resolveSize(height, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        ensureChildren();

        final TimeRulerView rulerView = mRulerView;
        mRulerView.setEndHour(55);
        rulerView.layout(0, 0, getWidth(), getHeight());
        
        if (isAdded == false) {
        	int k = 0;
        	String s = new String();
        	for( int j=0; j< schedule.getAllDay(); j++ )
        	{
        		s = "";
        		if( j < schedule.getNewDay() )
        		{
        			for( int i = 0; i < schedule.getListEveryday(); i++ )
        			{
        				s +=  schedule.getNewWord(j + 1)[i];
        				s += " ";
        			}
        			if(s!="")
        				placeBlock(s, j+1, j+2, 0, mRulerView);
        		}
        		s = "";
        		for ( k =0; k< schedule.getReviewWord(j+1).size(); k++)
        		{
        			if(k > 3)
        				break;
        			s += schedule.getReviewWord(j+1).get(k);
        			s += " ";
        		}
        		if(s!="")
        			placeBlock(s, j+1, j+2, 1, mRulerView);
        		s = "";
        		while( k < schedule.getReviewWord(j+1).size() )
        		{
        			s += schedule.getReviewWord(j+1).get(k);
        			s += " ";
        			k++;
        		}
        		if(s!="")
        			placeBlock(s, j+1, j+2, 2, mRulerView);
        	}
        	
        	isAdded = true;
        }
        // Align now view to match current time
        final View nowView = mNowView;
        final long now = 0 ;// UIUtils.getCurrentTime(getContext());

        final int top = rulerView.getTimeVerticalOffset(now);
        final int bottom = top + nowView.getMeasuredHeight();
        final int left = 0;
        final int right = getWidth();

        nowView.layout(left, top, right, bottom);
    }
    
    void placeBlock( String content, int startday, int endday, int column, TimeRulerView rulerView)
    {
    	int headerWidth = rulerView.getHeaderWidth();
    	int columnWidth = (getWidth() - headerWidth) / mColumns;
    	BlockView blockView = new BlockView(this.context, "5", content, startday, endday,
                false, column);

        blockView.setFocusable(false);
        blockView.setEnabled(true);
        LayerDrawable buttonDrawable = (LayerDrawable) blockView.getBackground();
        buttonDrawable.getDrawable(0).setAlpha(100);
        buttonDrawable.getDrawable(2).setAlpha(100);
        this.addBlock(blockView);
        final int top = rulerView.getTimeVerticalOffset(blockView.getStartTime());
        final int bottom = rulerView.getTimeVerticalOffset(blockView.getEndTime());
        final int left = headerWidth + (blockView.getColumn() * columnWidth);
        final int right = left + columnWidth;
        blockView.layout(left, top, right, bottom);
    }
}